package com.tiamo.traders.controller;

import com.tiamo.traders.entity.User;
import com.tiamo.traders.service.UserSeervice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.TimeoutUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

@RestController
public class UserLogin {

    @Autowired
    private UserSeervice userSeervice;

    @Autowired
    RedisTemplate redisTemplate;


    @PostMapping("/login")
    @ResponseBody
    public String userlogin(User user, HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //String sessionCode = (String) req.getSession().getAttribute("code");
        //从redis中获取code
        String codeheader=req.getHeader("codekey");
        String code=(String) redisTemplate.boundValueOps(codeheader).get();
        System.out.println(code);
        user.setSessionCode(code);
        String res=userSeervice.userLogin(user);
        resp.setHeader("Access-Control-Expose-Headers","token");
        resp.setHeader("token",user.getName());
        return res;
    }

}
